package tt.dz.egobus.servicemanager.core.task;

import tt.dz.egobus.servicemanager.bean.IdAlgorithm;
import tt.dz.egobus.servicemanager.business.convert.DynamicTaskNamespaceConvert;
import tt.dz.egobus.servicemanager.business.key.BusinessCache;
import tt.dz.egobus.servicemanager.business.task.job.QuartzDynamicTask;

import java.io.Serializable;

/**
 * @author Anson
 * @date 17-8-14
 * @name 动态任务命名空间
 * @since 1.0.6
 */
public class DynamicTaskNamespace implements Serializable {

    private static final long serialVersionUID = -9123185009874292935L;

    protected String jobName;
    protected String jobGroupName;
    protected String triggerName;
    protected String triggerGroupName;

    /**
     * @param cache 当前业务缓存
     * @author Anson
     * @date 17-8-15
     * @name 根据当前业务为缓存转换成一个动弹任务命名空间
     * @since 1.0.6
     */
    public static DynamicTaskNamespace dynamicTaskNamespaceByBusinessCache(BusinessCache cache,
                                                                           Class<? extends QuartzDynamicTask> clazz) {
        DynamicTaskNamespaceConvert convert = new DynamicTaskNamespaceConvert();
        convert.setDynamicExecutorTask(clazz);
        return convert.convert(cache);
    }

    public DynamicTaskNamespace() {

    }

    public DynamicTaskNamespace(String jobGroupName) {
        this.jobGroupName = jobGroupName;
    }

    public DynamicTaskNamespace(String jobName, String jobGroupName, String triggerName, String triggerGroupName) {
        this.jobName = jobName;
        this.jobGroupName = jobGroupName;
        this.triggerName = triggerName;
        this.triggerGroupName = triggerGroupName;
    }

    public DynamicTaskNamespace(DynamicTask task) {
        this(task.getJobName(), task.getJobGroupName(), task.getTriggerName(), task.getTriggerGroupName());
    }

    /**
     * @author Anson
     * @date 17-8-15
     * @name key生成器
     * @since 1.0.6
     */
    protected IdAlgorithm dynamicTaskGeneral() {
        return new DynamicTaskKeyGeneral(this.jobName, this.triggerName);
    }

    /**
     * @author Anson
     * @date 17-8-4
     * @name 获取当前缓存的key
     * @since 1.0.6
     */
    public String key() {
        return this.dynamicTaskGeneral().id();
    }

    public String getJobName() {
        return jobName;
    }

    public void setJobName(String jobName) {
        this.jobName = jobName;
    }

    public String getJobGroupName() {
        return jobGroupName;
    }

    public void setJobGroupName(String jobGroupName) {
        this.jobGroupName = jobGroupName;
    }

    public String getTriggerName() {
        return triggerName;
    }

    public void setTriggerName(String triggerName) {
        this.triggerName = triggerName;
    }

    public String getTriggerGroupName() {
        return triggerGroupName;
    }

    public void setTriggerGroupName(String triggerGroupName) {
        this.triggerGroupName = triggerGroupName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof DynamicTaskNamespace)) return false;

        DynamicTaskNamespace that = (DynamicTaskNamespace) o;
        return this.hashCode() == that.hashCode();
    }

    @Override
    public int hashCode() {
        int result = this.key().hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "DynamicTaskNamespace{" +
                "jobName='" + jobName + '\'' +
                ", jobGroupName='" + jobGroupName + '\'' +
                ", triggerName='" + triggerName + '\'' +
                ", triggerGroupName='" + triggerGroupName + '\'' +
                '}';
    }
}
